package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/15.
 */

import com.xianzhiOA.bean.*;
import com.xianzhiOA.service.*;
import com.xianzhiOA.util.PageBean;
import javafx.beans.property.SimpleMapProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 申请基础表的信息
 *
 * @author 王诗博
 * @create 2017-05-15 17:37
 **/
@Controller
@RequestMapping("/XzTempletBaseInfo")
@SessionAttributes("userLogin")
public class XzTempletBaseInfoController {
    @Autowired
    private XzTempletBaseInfoService templetBaseInfoService;
    @Autowired
    private XzTempletBaseService templetBaseService;
    @Autowired
    private XzTempletExtendService templetExtendService;
    @Autowired
    private XzTempletExtendInfoService templetExtendInfoService;
    @Autowired
    private XzApprovalProcessInfoService approvalProcessInfoService;

    @RequestMapping("/structureTemplet.emp")
    public ModelAndView structureTemplet(Long templetBaseId){
       ModelAndView mv=new ModelAndView("backEnd/templetInfoAdd");
        System.out.println("templetBaseId:"+templetBaseId);
        //基础模板中的数据
        XzTempletBase templetBase=  templetBaseService.selAllById(templetBaseId);
        List<XzTempletExtend> templetExtendList=templetExtendService.selAllByBaseId(templetBaseId);
        System.out.println("templetExtendList:"+templetExtendList);
        templetBase.setExtendObjectList(templetExtendList);
        System.out.println("templetBase:"+templetBase);
        System.out.println("templetBase:"+templetBase.getTempletBaseId());
        mv.addObject("templetBase",templetBase);
       return mv;
    }

    @RequestMapping("/choiceBaseTemplet.emp")
    public ModelAndView choiceBaseTemplet(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue ="10") int rows,@ModelAttribute("userLogin")XzEmp emp,String beginStr, String  endStr,XzTempletBaseInfo templetBaseInfo){
        ModelAndView mv=new ModelAndView("backEnd/templetChoice");
        //查询所有基础模板
        List<XzTempletBase> templetBaseList=templetBaseService.selTempletBase();
        templetBaseInfo.setApplicant(emp.getEmpNo());  //获取登录的员工工号
          if(templetBaseInfo.getAuditStatus() != null && templetBaseInfo.getAuditStatus() != "") {
              if (templetBaseInfo.getAuditStatus().equals("4")) {
                  String auditStatus = templetBaseInfo.getAuditStatus();
                  templetBaseInfo.setAuditStatus(null);
              }
              mv.addObject("auditStatus",templetBaseInfo.getAuditStatus());
          }


       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {

            if(beginStr != null && endStr != null) {
            Date begin=null;
            Date end=null;
            begin=sdf.parse(beginStr);
            end=sdf.parse(endStr);
                if (begin.getTime() > end.getTime()) {
                    mv.addObject("msg", "开始时间不能大于结束时间");
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }




        PageBean<XzTempletBaseInfo> pageBean=templetBaseInfoService.selAll(templetBaseInfo,page,rows,beginStr,endStr);
        List<XzTempletBaseInfo> templetBaseInfoList = pageBean.getList();
        mv.addObject("templetBaseInfoList", templetBaseInfoList);
        mv.addObject("templetBaseInfo",templetBaseInfo);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getSize());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("templetBaseList",templetBaseList);
        mv.addObject("beginStr",beginStr);
        mv.addObject("endStr",endStr);

        return mv;
    }

    //保存提交的申请信息
    @RequestMapping("/saveEmpletBaseInfo.emp")
    public ModelAndView saveEmpletBaseInfo(XzTempletBaseInfo templetBaseInfo, @ModelAttribute("userLogin")XzEmp emp){
        ModelAndView mv=new ModelAndView("redirect:choiceBaseTemplet.emp");
        templetBaseInfo.setAuditStatus("0");   //将新提交的申请状态设置为0（0：代表处理中）
        Date date=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataStr=templetBaseInfo.getApplicantTimeStr();
        try {
            date=sdf.parse(dataStr);
            System.out.println("date:"+date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        templetBaseInfo.setApplicantTime(date);
        templetBaseInfo.setApplicant(emp.getEmpNo());
        System.out.println("templetBaseInfo:"+templetBaseInfo);
        //添加基础模板的实例表
        Long templetBaseInfoId= templetBaseInfoService.insert(templetBaseInfo);
        //添加扩装实例表
        templetBaseInfo.setTempletBaseInfoId(templetBaseInfoId); //把查询出来的基础模板实例的ID赋给对象，让其添加到扩装实例表
        int b=templetExtendInfoService.insert(templetBaseInfo);
        System.out.println("b:"+b);
        //添加节点实例表
        int c=  approvalProcessInfoService.insert(templetBaseInfo.getTempletBaseId(),templetBaseInfoId);
        return mv;
    }

   //撤回申请（删除申请）
    @RequestMapping("/deleteEmpletBaseInfo.emp")
    public ModelAndView deleteEmpletBaseInfo(Long templetBaseInfoId){
        ModelAndView mv=new ModelAndView("redirect:choiceBaseTemplet.emp");
        System.out.println("templetBaseInfo对象:"+templetBaseInfoId);
//        XzTempletBaseInfo templetBaseInfo=templetBaseInfoService.selectByPrimaryKey(templetBaseInfoId);
        Date newtime = new Date();
        //伪删除基本实例表
        XzTempletBaseInfo templetBaseInfo=new XzTempletBaseInfo();
        templetBaseInfo.setDeleteTime(newtime);
        templetBaseInfo.setTempletBaseInfoId(templetBaseInfoId);
        int a= templetBaseInfoService.phoneyDeleteByPrimaryKey(templetBaseInfo);
        //删除扩展实例表
        XzTempletExtendInfo templetExtendInfo=new XzTempletExtendInfo();
        templetExtendInfo.setDeleteTime(newtime);
        templetExtendInfo.setTempletBaseInfoId(templetBaseInfoId);
        int b=templetExtendInfoService.phoneyDeleteByPrimaryKey(templetExtendInfo);
        //删除节点实例表
        XzApprovalProcessInfo approvalProcessInfo=new XzApprovalProcessInfo();
        approvalProcessInfo.setDeleteTime(newtime);
        approvalProcessInfo.setTempletBaseInfoId(templetBaseInfoId);
        int c=approvalProcessInfoService.phoneyDeleteByPrimaryKey(approvalProcessInfo);
        return mv;
    }


    @RequestMapping("updateState.emp")
    public ModelAndView updateState(Long templetBaseInfoId,String auditStatus){
        ModelAndView mv=new ModelAndView("redirect:choiceBaseTemplet.emp");
      //  selectByPrimaryKey
        XzTempletBaseInfo templetBaseInfo=templetBaseInfoService.selectByPrimaryKey(templetBaseInfoId);
        String newApproval="0";
        if(templetBaseInfo.getNewApproval() != null && templetBaseInfo.getNewApproval() != ""){
            newApproval=templetBaseInfo.getNewApproval();
        }
        if(templetBaseInfo.getAuditStatus() != auditStatus){
            newApproval="1";
        }
        //修改基础实例表中的申请状态
        int a=templetBaseInfoService.updateState(templetBaseInfoId,auditStatus,newApproval);
        //修改节点实例表中的数据
        int b=approvalProcessInfoService.updateState(templetBaseInfoId,auditStatus);
        return mv;
    }

    //跳到申请的详情页面
     @RequestMapping("toDetails.emp")
    public ModelAndView toDetails(Long templetBaseInfoId){
            ModelAndView mv=new ModelAndView("backEnd/templetDetails");
            XzTempletBaseInfo templetBaseInfo=templetBaseInfoService.selectByPrimaryKey(templetBaseInfoId);
            templetBaseInfo.setExtendInfoObjectList(templetExtendInfoService.selectByPrimaryKey(templetBaseInfoId));
         templetBaseInfo.setApprovalProcessInfoObjectList(approvalProcessInfoService.selectByPrimaryKey(templetBaseInfoId));

         System.out.println("templetBaseInfo:"+templetBaseInfo);
            mv.addObject("templetBaseInfo",templetBaseInfo);
            return mv;
     }

     //申请详情的，某个角色回退功能
    @RequestMapping("approvalFallback.emp")
    public ModelAndView approvalFallback(Long templetBaseInfoId,Long approvalprocessInfoId){
        ModelAndView mv=new ModelAndView("redirect:toDetails.emp?templetBaseInfoId="+templetBaseInfoId);
        XzApprovalProcessInfo approvalProcessInfo=approvalProcessInfoService.selectById(approvalprocessInfoId);
        //修改节点表中的指定审批节点改为处理中
        int a=approvalProcessInfoService.updateStateToInProcess(templetBaseInfoId,approvalProcessInfo.getNodeId());
        //修改基础模板实例表中的审批状态，改为处理中
        XzTempletBaseInfo templetBaseInfo=templetBaseInfoService.selectByPrimaryKey(templetBaseInfoId);
        String newApproval="0";
        String auditStatus="0";  //基础模板实例表中的状态设置为0（处理中）
        if(templetBaseInfo.getNewApproval() != null && templetBaseInfo.getNewApproval() != ""){
            newApproval=templetBaseInfo.getNewApproval();
        }
        if(templetBaseInfo.getAuditStatus() != auditStatus){
            newApproval="1";
        }
        //修改基础实例表中的申请状态
        int b=templetBaseInfoService.updateState(templetBaseInfoId,auditStatus,newApproval);
        return mv;
    }


}
